//-----------------------------------------------------------------------
// <copyright file="QuangCaoDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using APPro.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace APPro.Data
{    
    public abstract class QuangCaoDAOBase
    {
        #region Common methods
        public virtual QuangCao CreateQuangCaoFromReader(IDataReader reader)
        {
            QuangCao item = new QuangCao();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("MaQuangCao"))) item.MaQuangCao = (int)reader["MaQuangCao"];
                if (!reader.IsDBNull(reader.GetOrdinal("TenQuangCao"))) item.TenQuangCao = (string)reader["TenQuangCao"];
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDung"))) item.NoiDung = (string)reader["NoiDung"];
                if (!reader.IsDBNull(reader.GetOrdinal("ChiTiet"))) item.ChiTiet = (string)reader["ChiTiet"];
                if (!reader.IsDBNull(reader.GetOrdinal("ThuocDanhMuc"))) item.ThuocDanhMuc = (int)reader["ThuocDanhMuc"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateQuangCaoFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreateQuangCao methods
            
        public virtual void CreateQuangCao(QuangCao quangCao)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spQuangCaoInsert");
                
				database.AddInParameter(dbCommand, "@MaQuangCao", DbType.Int32, quangCao.MaQuangCao);
				database.AddInParameter(dbCommand, "@TenQuangCao", DbType.StringFixedLength, quangCao.TenQuangCao);
				database.AddInParameter(dbCommand, "@NoiDung", DbType.StringFixedLength, quangCao.NoiDung);
				database.AddInParameter(dbCommand, "@ChiTiet", DbType.StringFixedLength, quangCao.ChiTiet);
				database.AddInParameter(dbCommand, "@ThuocDanhMuc", DbType.Int32, quangCao.ThuocDanhMuc);
				
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateQuangCaoException, ex);
            }
        }

        #endregion
		
        #region UpdateQuangCao methods
        
        public virtual void UpdateQuangCao(QuangCao quangCao)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spQuangCaoUpdate");            
                
                database.AddInParameter(dbCommand, "@MaQuangCao", DbType.Int32, quangCao.MaQuangCao);
                database.AddInParameter(dbCommand, "@TenQuangCao", DbType.StringFixedLength, quangCao.TenQuangCao);
                database.AddInParameter(dbCommand, "@NoiDung", DbType.StringFixedLength, quangCao.NoiDung);
                database.AddInParameter(dbCommand, "@ChiTiet", DbType.StringFixedLength, quangCao.ChiTiet);
                database.AddInParameter(dbCommand, "@ThuocDanhMuc", DbType.Int32, quangCao.ThuocDanhMuc);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdateQuangCaoException, ex);
            }
        }
        
        #endregion
		
        #region DeleteQuangCao methods
        public virtual void DeleteQuangCao(int maQuangCao)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spQuangCaoDelete");
                
                database.AddInParameter(dbCommand, "@MaQuangCao", DbType.Int32, maQuangCao);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeleteQuangCaoException, ex);
            }
        }

        #endregion
		
        #region GetQuangCao methods
        
        public virtual QuangCao GetQuangCao(int maQuangCao)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spQuangCaoGet");
                
                database.AddInParameter(dbCommand, "@MaQuangCao", DbType.Int32, maQuangCao);
                
                QuangCao quangCao =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        quangCao = CreateQuangCaoFromReader(reader);
                        reader.Close();
                    }
                }
                return quangCao;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetQuangCaoException, ex);
            }
        }
        
        #endregion
		
        #region GetQuangCaoList methods
        public virtual QuangCaoCollection GetQuangCaoList(QuangCaoColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spQuangCaoGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                QuangCaoCollection quangCaoCollection = new QuangCaoCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        QuangCao quangCao = CreateQuangCaoFromReader(reader);
                        quangCaoCollection.Add(quangCao);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return quangCaoCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetQuangCaoListException, ex);
            }
        }
        
        public virtual QuangCaoCollection GetQuangCaoList(QuangCaoColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetQuangCaoList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}